//
//  WorkDayVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/24.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "WorkDayVC.h"
#import "DatePickerView.h"
#import "FDAlertView.h"
#import "DatePickerView.h"
#import "WorkTimeCell.h"
@interface WorkDayVC ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation WorkDayVC{
    
    NSString *_checkDate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake((kScreenWidth-130)/2, 10, 130, 30)];
    textField.text = @"请选择日期";
    textField.textAlignment = NSTextAlignmentCenter;
    textField.delegate = self;
    [self setRightViewWithTextField:textField imageName:@"日历"];
    [self.view addSubview:textField];
   
    self.tbView.frame = CGRectMake(0, 40, kScreenWidth, kScreenHeight);
    [self.view addSubview:self.tbView];
    self.tbView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tbView registerNib:[UINib nibWithNibName:@"WorkTimeCell" bundle:nil] forCellReuseIdentifier:@"WorkTimeCell"];
    //获取当前时间,日期
    [self.tbView setSeparatorInset:UIEdgeInsetsZero];
    
    [self.tbView setLayoutMargins:UIEdgeInsetsZero];
}

- (UITableView *)tbView{
    
    if ( !_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
    }
    return _tbView;
}
-(void)setRightViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    rightView.image = [UIImage imageNamed:imageName];
    rightView.size = CGSizeMake(40, 40);
    rightView.contentMode = UIViewContentModeCenter;
    textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    FDAlertView *alert = [[FDAlertView alloc] init];
    DatePickerView * contentView=[[DatePickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    contentView.year = self.year;
    contentView.month = self.month;
    contentView.delegate=self;
    alert.contentView = contentView;
    [alert show];
    return NO;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkTimeCell"];
    if (indexPath.section == 0) {
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
   
    [cell setCellWithData:[self.dataSource objectAtIndex:indexPath.row]];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    return self.dataSource.count;
}

-(void)getTimeToValue:(NSString *)theTimeStr{
    
    _checkDate = theTimeStr;
    [self requestDataWithDate:_checkDate];
    
}

- (void)requestDataWithDate:(NSString *)date{
    
    NSString *url = [QParamService urlWithIOTAction:@"report/dayWorktime"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:_checkDate forKey:@"datetime"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"dic:%@",basModel.items);
           self.dataSource =  [basModel.items objectForKey:@"reqportDeviceData"];
            [self.tbView reloadData];
        }
    }];
}

ARRAY_INIT(dataSource);
@end
